package com.library_management.dao.impl;

import com.library_management.dao.BookDao;
import com.library_management.model.Book;  // 修正实体类包路径（原pojo改为model）
import com.library_management.jdbc.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public abstract class BookDaoImpl implements BookDao {

    @Override
    public Book selectById(Integer id) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Book book = null;

        try {
            // 获取连接并判断非空
            conn = JdbcUtil.getConnection();
            if (conn == null) {
                System.err.println("获取数据库连接失败：conn为null");
                return null;
            }

            // 修正SQL语法（补全字段和条件）
            String sql = "SELECT id, bookname, author, isbn, publisher, publish_date, stock, available " +
                    "FROM book WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);  // 正确绑定参数索引

            rs = pstmt.executeQuery();
            if (rs.next()) {
                book = new Book();
                book.setId(rs.getInt("id"));
                book.setBookname(rs.getString("bookname"));
                book.setAuthor(rs.getString("author"));
                book.setIsbn(rs.getString("isbn"));
                book.setPublisher(rs.getString("publisher"));
                book.setPublishDate(rs.getDate("publish_date"));
                book.setStock(rs.getInt("stock"));
                book.setAvailable(rs.getInt("available"));
            }

        } catch (SQLException e) {
            e.printStackTrace();  // 暂用打印异常，后续可替换为日志
        } finally {
            // 关闭资源（参数顺序正确：rs, pstmt, conn）
            JdbcUtil.close(rs, pstmt,conn);
        }

        return book;
    }

    @Override
    public List<Book> selectAvailableBooks() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Book> bookList = new ArrayList<>();

        try {
            conn = JdbcUtil.getConnection();
            if (conn == null) {
                System.err.println("获取数据库连接失败：conn为null");
                return bookList;
            }

            // 补全SQL并修正语法
            String sql = "SELECT id, bookname, author, isbn, publisher, publish_date, stock, available " +
                    "FROM book WHERE available > 0";
            pstmt = conn.prepareStatement(sql);

            rs = pstmt.executeQuery();
            while (rs.next()) {
                Book book = new Book();
                book.setId(rs.getInt("id"));
                book.setBookname(rs.getString("bookname"));
                book.setAuthor(rs.getString("author"));
                book.setIsbn(rs.getString("isbn"));
                book.setPublisher(rs.getString("publisher"));
                book.setPublishDate(rs.getDate("publish_date"));
                book.setStock(rs.getInt("stock"));
                book.setAvailable(rs.getInt("available"));
                bookList.add(book);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(rs, pstmt,conn);  // 正确关闭资源
        }

        return bookList;
    }

    // 补充接口中可能缺失的selectByCondition方法（根据错误提示）
    public List<Book> selectByCondition(String condition) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Book> bookList = new ArrayList<>();

        try {
            conn = JdbcUtil.getConnection();
            if (conn == null) {
                System.err.println("获取数据库连接失败：conn为null");
                return bookList;
            }

            // 示例：根据条件查询（需根据实际业务调整SQL）
            String sql = "SELECT id, bookname, author, isbn, publisher, publish_date, stock, available " +
                    "FROM book WHERE " + condition;  // 注意SQL注入风险，实际需参数化
            pstmt = conn.prepareStatement(sql);

            rs = pstmt.executeQuery();
            while (rs.next()) {
                Book book = new Book();
                book.setId(rs.getInt("id"));
                book.setBookname(rs.getString("bookname"));
                bookList.add(book);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(rs, pstmt,conn);
        }

        return bookList;
    }
}